<?php

namespace common\models;

/**
 * This is the model class for table "RecruitApplicant".
 */
class RecruitApplicant extends ActiveRecord {

    // @name 状态
    const StatusExamine = 0;
    const StatusRevoke = 20;
    const StatusAccept = 10;
    const StatusRefuse = 11;
    public static $statusSelector = [
        self::StatusExamine => ['title' => '申请中', 'status' => 'orange'],
        self::StatusRevoke => ['title' => '已撤销', 'status' => 'red'],
        self::StatusAccept => ['title' => '已雇佣', 'status' => 'green'],
        self::StatusRefuse => ['title' => '已拒绝', 'status' => 'red'],
    ];

    // only define rules for those attributes that
    // will receive user inputs.
    public function rules()
    {
        return [
            [['recruit_id', 'designer_id'], 'required'],
            [['recruit_id', 'designer_id', 'accept_at', 'status'], 'integer'],
            [['remark'], 'string', 'max' => 255],
        ];
    }
    /**
     * @name 字段名称
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'recruit_id' => '招聘编号',
            'designer_id' => '设计师',
            'accept_at' => '接受时间',
            'status' => '状态',
        ];
    }

    /**
     * get tags
     */
    public function getDesigner()
    {
        return $this->hasOne(User::className(), ['id' => 'designer_id']);
    }

    /**
     * get recruit
     */
    public function getRecruit()
    {
        return $this->hasOne(Recruit::className(), ['id' => 'recruit_id']);
    }

    /**
     * 撤销投递
     * @return boolean
     */
    public function revoke()
    {
        if( ! in_array($this->status, [static::StatusExamine])) {
            return false;
        }
        $this->status = static::StatusRevoke;
        $this->updated_at = time();
        return $this->save();
    }

    /**
     * 雇佣设计师
     * @param $amount int 雇佣金额
     * @return mixed
     */
    public function accept($amount)
    {
        if($amount <= 0) {
            return false;
        }
        $this->accept_at = time();
        $this->status = static::StatusAccept;
        return $this->transaction(function($db) use ($amount) {
            if( ! $this->save()) {
                throw new \Exception('update recruit applicant error');
            }
            $params = [
                'recruit_id' => $this->id,
                'employer_id' => $this->recruit->employer_id,
                'designer_id' => $this->designer_id,
                'type' => $this->recruit->type,
                'period' => $this->recruit->period,
                'hires' => $amount,
                'remark' => '接受用户的简历投递',
            ];
            // 建立雇佣关系
            if( ! Employment::employ($params)) {
                throw new \Exception('update employment error');
            }
            return true;
        });
    }

    /**
     * 投递简历
     * @param $params array 投递参数
     * @return mixed
     */
    public static function creator($params)
    {
        if(empty($params)) {
            return false;
        }
        $applicant = new static();
        $applicant->loads($params);
        $applicant->status = static::StatusExamine;
        if( ! ($applicant->validate() && $applicant->save())) {
            return false;
        }
        return $applicant;
    }
}